
package main

import (
	"fmt"
)

var user=""

func inita(){
	defer func(){
		fmt.Println("defer ### ")
	}()
	if user=="" {
		fmt.Println("before panic")
		panic("no value for user")
		fmt.Println("afete panic")
	}
}

func throwPanic(f func())(b bool){
	defer func(){
		if x:=recover();x!=nil{
			fmt.Println("rrrr - ",x)
			b=true
		}
	}()
	f()
	fmt.Println("after func run")
	return
}

func main(){
//	throwPanic(inita)
	test3();
}

func test1(){
	fmt.Println("enter test1")
	defer func(){
		fmt.Println("enter defer...")
		if x:=recover();x!=nil {
			fmt.Println("enter recover..." , x)
			fmt.Printf("type of recover return: %T\n",x)
		}
		fmt.Println("end of defer")
	}()
	var i int=5
	if i%3 == 2 {
		panic("hello panic 1 ")
	}
	fmt.Println("end of test1 / normal exit")
}

func test2(){
	fmt.Println("enter test2")
	defer func(){
		fmt.Println("enter test2 defer")
		recover()
		fmt.Println("end of test2 defer")
	}()
	test2_2()
	fmt.Println("end of test2")
}
func test2_2(){
	fmt.Println("enter test2_2")
	panic("panic of test2_2")
	fmt.Println("end of test2_2")
}

type re struct{
	name string
	code int
}
func test3(){
	fmt.Println("enter test3")
	defer func(){
		fmt.Println("enter test3 defer")
		if x:=recover();x!=nil {
			fmt.Println("enter recover")
			fmt.Printf("Type of recover %T: \n",x)
			fmt.Println("end of recover")
		}
		fmt.Println("end of test3 defer")
	}()
	if i:=5;i%3==2 {
		fmt.Println("pre panic")
		panic(re{name:"panic",code:2})
	}
	fmt.Println("end of test3 / normal exit")
}